// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Reflection;
using ValidationAspects.Sdk;

namespace ValidationAspects.Configuration
{
	public class PropertyValidation : IPropertyValidation
	{
		public string Property { get; set; }
		public ValidationApplication Apply { get; set; }
		public List<IValidatorFactory> Validators { get; set; }

		public PropertyValidation()
		{
			Validators = new List<IValidatorFactory>();
		}

		public void Register(Type type)
		{
			Enforce.IsNotNull(type, "type");

			PropertyInfo propertyInfo = type.GetProperty(Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

			if (propertyInfo == null)
				throw new ValidationConfigurationException(string.Format("Property [name:{0}] [type:{1}] is unknown.", Property, type));

			if (Apply == ValidationApplication.Replace)
				propertyInfo.ReplaceValidation(Validators);
			else
				propertyInfo.AddValidation(Validators);
		}
	}
}